package com.sjm.suchbear.minibookmall;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjm.suchbear.minibookmall.Utils.SP;
import com.sjm.suchbear.minibookmall.View.MyListView;
import com.sjm.suchbear.minibookmall.action.OrderManage;
import com.sjm.suchbear.minibookmall.model.OrderModel;
import com.sjm.suchbear.minibookmall.model.OrderState;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class OrderListActivity extends Activity {

    @BindView(R.id.ll_header_back)
    LinearLayout llHeaderBack;
    @BindView(R.id.tv_list_title)
    TextView tvListTitle;
    @BindView(R.id.iv_header_function_ico)
    ImageView ivHeaderFunctionIco;
    @BindView(R.id.ll_header_function)
    LinearLayout llHeaderFunction;
    @BindView(R.id.ll_List_Main)
    RelativeLayout llListMain;
    @BindView(R.id.lv_orderList)
    MyListView lvOrderList;

    private int model;  //0：全部订单 1：待付款 2：已付款
    private List<OrderModel> orderModelList;
    private SQLiteDatabase dbread;
    private OrderAdapter orderAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_list);
        ButterKnife.bind(this);
        BookDB db = new BookDB(getBaseContext());
        dbread = db.getReadableDatabase();
        model = getIntent().getIntExtra("model", 0);
    }

    @Override
    protected void onResume() {
        super.onResume();
        switch (model) {
            case 1:
                tvListTitle.setText("待付款");
                orderModelList = OrderManage.getOrderListbyid(dbread, SP.getUid(getBaseContext()), OrderState.WAITPAY);
                break;
            case 2:
                tvListTitle.setText("待收货");
                orderModelList = OrderManage.getOrderListbyid(dbread, SP.getUid(getBaseContext()), OrderState.HADPAY);
                break;
            case 0:
                tvListTitle.setText("我的订单");
                orderModelList = OrderManage.getOrderListbyid(dbread, SP.getUid(getBaseContext()));
                break;
        }
        orderAdapter = new OrderAdapter(getBaseContext(), orderModelList);
        lvOrderList.setAdapter(orderAdapter);
        lvOrderList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent = new Intent(OrderListActivity.this, OrderDetailActivity.class);
                intent.putExtra("orderID", orderModelList.get(i).getOrderID());
                startActivity(intent);
            }
        });
    }

    @OnClick(R.id.ll_header_back)
    public void onViewClicked() {
        finish();
    }
}
